using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.IO;

namespace CodeFactory.Library.Language
{
    public class LanguageManager
    {
        private static string languageFile;
        private static string languagePath;
        private static string languageClass;
        private static IStringManager stringManager;
        private static List<LanguageInfo> packList;

        private static object lockObj = new object();

        public static string LanguageFile
        {
            get { return languageFile; }
        }

        public static string LanguageClass
        {
            get { return languageClass; }
        }

        public static IStringManager StringManager
        {
            get { return stringManager; }
        }

        public static List<LanguageInfo> PackList
        {
            get { return packList; }
        }

        static LanguageManager()
        {
            try
            {
                languageFile = string.Empty;
                languagePath = System.Environment.CurrentDirectory + "\\Languages\\";
                languageClass = string.Empty;
                stringManager = null;
                packList = new List<LanguageInfo>();
                lock (lockObj)
                {
                    LoadAllLanguage();
                    ReloadLanguageManager();
                }
            }
            catch (Exception e)
            {
                LibraryMessageBox.ShowErrorAndExit("LanguageManager Initialization Error", e.Message);
            }
        }

        public static void ReloadLanguageManager()
        {
            lock (lockObj)
            {
                InitDefaultLanguage();
                InitStringManager();
                LoadAllLanguage();
            }
        }

        private static void InitDefaultLanguage()
        {
            languageFile = CodeFactory.Library.Config.ConfigManager.Info.LanguagePack;
            languageClass = languageFile + ".StringManager";
        }

        private static void LoadAllLanguage()
        {
            DirectoryInfo rootDir = new DirectoryInfo(languagePath);
            if (!rootDir.Exists)
            {
                throw new Exception("Can't open language forder.");
            }
            IStringManager manager = null;
            string packName;
            LanguageInfo langInfo;
            packList.Clear();
            foreach (FileInfo f in rootDir.GetFiles())
            {
                if (f.Name.EndsWith(".dll"))
                {
                    packName = f.Name.Substring(0, f.Name.IndexOf(".dll"));
                }
                else
                {
                    packName = f.Name;
                }
                manager = Assembly.LoadFile(f.FullName).CreateInstance(packName + ".StringManager") as IStringManager;
                if (manager != null)
                {
                    langInfo = new LanguageInfo();
                    langInfo.LangName = manager.Name;
                    langInfo.LangPack = packName;
                    packList.Add(langInfo);
                }
            }
        }

        private static void InitStringManager()
        {
            try
            {
                lock (lockObj)
                {
                    stringManager = (IStringManager)Assembly.LoadFile(languagePath + languageFile + ".dll").CreateInstance(languageClass);
                }
            }
            catch (Exception)
            {
                throw new Exception("Can't load default language package.");
            }
        }
    }
}
